package cn.golxen.goods.service.impl;

import cn.golxen.commons.entity.Result;
import cn.golxen.commons.exception.GoodsAddException;
import cn.golxen.commons.exception.GoodsUpdateException;
import cn.golxen.commons.vo.BarcodeInfoVo;
import cn.golxen.commons.vo.CounterVO;
import cn.golxen.commons.vo.GoodsImageVo;
import cn.golxen.goods.entity.Goods;
import cn.golxen.goods.feign.BarcodeFeign;
import cn.golxen.goods.mapper.GoodsMapper;
import cn.golxen.goods.service.GoodsService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
@Service
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    GoodsMapper goodsMapper;
    @Autowired
    BarcodeFeign barcodeFeign;

    private static final String defaultUrl="http://bt.golxen.cn:15357/down/ijnXgaRVvBU5";

    /*扫描事件*/
    @Override
    public Result ScanEvents(String url,String userId) {
        String code=barcodeFeign.getCode(url);
        if (code.equals("{\"code\":1,\"message\":\"条形码识别错误，请确保条形码正确或重试！\",\"data\":null}"))
            return new Result(1,"条形码识别错误，请确保条形码正确或重试！");
        if (getGoodsByCode(code,userId)==null){
            return new Result(201,"添加商品",getGoodsByBarcode(code,userId));
        }else
            return new Result(200,"查看商品",getGoodsByCode(code,userId));
    }

    @Override
    public List<Goods> selectGoodsByCategoryId(Integer id,String userId) {
        return goodsMapper.selectGoodsByCategoryId(id,userId);

    }

    @Override
    public BarcodeInfoVo getGoodsByBarcode(String code,String userId) {
        return barcodeFeign.getBarcodeInfoByCode(code);
    }

    @Override
    public Goods getGoodsByCode(String code,String userId) {
        return goodsMapper.getGoodsByCode(code,userId);
    }

    @Override
    public Goods addGoods(Goods goods,String userId) {
        if (goods.getGoodsInventory()==0)
            goods.setGoodsStatus(2);
        else if (goods.getGoodsInventory()<20)
            goods.setGoodsStatus(1);
        else goods.setGoodsStatus(0);

        if (goods.getUrl()==null||goods.getUrl().equals(""))
            goods.setUrl(defaultUrl);

        LocalDateTime localDateTime = LocalDateTime.now();
        goods.setLastTime(localDateTime);
        if (goodsMapper.addGoods(goods,userId)!=0)
            return goods;
        else throw new GoodsAddException();

    }

    @Override
    public Goods updateGoods(Goods goods,String userId) {
        Integer res;
        Goods resGoods=goodsMapper.getGoodsByCode(goods.getCode(),userId);
        if (resGoods.getGoodsInventory()!=goods.getGoodsInventory()) {
            if (goods.getGoodsInventory() == 0)
                goods.setGoodsStatus(2);
            else if (goods.getGoodsInventory() < 20)
                goods.setGoodsStatus(1);
            else goods.setGoodsStatus(0);
        }
        if (resGoods.getGoodsInventory()<goods.getGoodsInventory()){
            LocalDateTime localDateTime = LocalDateTime.now();
            goods.setLastTime(localDateTime);
        }


        res=goodsMapper.updateGoods(goods,userId);
        if (res==0)
            throw new GoodsUpdateException();
        resGoods=goodsMapper.getGoodsByCode(goods.getCode(),userId);
        return resGoods;
    }

    @Override
    public Integer deleteGoods(List<String> code,String userId) {
        return goodsMapper.deleteGoods(code,userId);
    }

    @Override
    public List<Goods> selectGoodsByGoodsName(String name,String userId) {
        return goodsMapper.selectGoodsByGoodsName(name,userId);
    }

    @Override
    public Integer updateGoodsImage(GoodsImageVo goodsImageVo,String userId) {
        return goodsMapper.updateGoodsImage(goodsImageVo,userId);
    }

    @Override
    public List<Goods> getGoodsList(String userId) {
        return goodsMapper.getGoodsList(userId);
    }

    @Override
    public Integer updateGoodsInventory(List<CounterVO> counterVO,String userId) {
        return goodsMapper.updateGoodsInventory(counterVO,userId);
    }

    @Override
    public List<Goods> getGoodssByCode(String code, String userId) {
        return goodsMapper.getGoodssByCode(code,userId);
    }
}
